package com.atguigu.srb.core.controller.admin;


import com.atguigu.common.exception.BusinessException;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.entity.IntegralGrade;
import com.atguigu.srb.core.service.impl.IntegralGradeServiceImpl;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author sofia
 * @since 2021-09-17
 */
@RestController
@CrossOrigin  //跨域
@Slf4j
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {

    @Autowired
    private IntegralGradeServiceImpl integralGradeService;

    @GetMapping("/getAllIntegerGrade")
    public R getAllIntegerGrade(){
        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list",list).setMessage("获取列表成功");
    }

    /**
     * @Author sofia
     * @Description //根据id删除
     * @Date 21:00 2021/9/20
     * @Param
     * @return
     **/
    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@PathVariable Long id){
        boolean b = integralGradeService.removeById(id);
        if (b){
            return R.ok().setMessage("删除成功");
        }else {
            return R.Error().setMessage("删除失败");
        }

    }

    /**
     * @Author sofia
     * @Description 新增积分等级
     * @Date  2021/9/20
     **/
    @PostMapping("/saveIntegralGrade")
    public R saveIntegralGrade(@RequestBody IntegralGrade integralGrade){
        if (integralGrade.getBorrowAmount()==null){
            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        }
        boolean save = integralGradeService.save(integralGrade);
        if (save){
            return R.ok().setMessage("添加成功");
        }else {
            return R.Error().setMessage("添加失败");
        }
    }
    /**
     * @Author sofia
     * @Descriptionq
     * @Date  2021/9/20
     **/
    @ApiOperation("根据Id获取积分等级")
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable Long id){

        IntegralGrade integralGrade = integralGradeService.getById(id);
        if (integralGrade!=null){
            return R.ok().data("record",integralGrade);
        }else {
            return R.Error().setMessage("获取信息失败");
        }
    }
    /*@GetMapping("getById/{id}")
    public R getById( @PathVariable Long id) {
        IntegralGrade integralGrade = integralGradeService.getById(id);
        if(integralGrade == null) {
            return R.Error().setMessage("根据id查询积分等级失败");
        }
        return R.ok().setMessage("根据id查询积分等级成功").data("data", integralGrade);
    }*/

    @PutMapping("/update")
    public R updateIntegralGrade(@RequestBody IntegralGrade integralGrade){
        boolean save = integralGradeService.updateById(integralGrade);
        if (save){
            return R.ok().setMessage("修改成功");
        }else {
            return R.Error().setMessage("修改失败");
        }
    }


}

